package com.example.bill.adapter;

import static android.util.TypedValue.COMPLEX_UNIT_DIP;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 默认选中第0条，是默认选中今天
 */
public class MonthDayAdapter extends BaseAdapter {

    private final Context context;
    private final DateTimeFormatter dateTimeFormatter;

    public MonthDayAdapter(Context context) {
        this.context = context;
        dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd");
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public LocalDateTime getItem(int position) {
        return LocalDateTime.now().minusDays(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView result;
        if (convertView == null) {
            result = new TextView(context);
            result.setTextSize(COMPLEX_UNIT_DIP, 20);
            /*setLayoutParams
            一旦设置布局，那么选中之后就会不显示

            if (parent == null || parent.getLayoutParams() == null) {
                result.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            } else {

                result.setLayoutParams(parent.getLayoutParams());
            }*/
            //result.setGravity(Gravity.CENTER);
           result.setPadding(10,10,10,10);
        } else {
            result = (TextView) convertView;
            Log.d("MonthDayAdapter", "复用成功");
        }

        result.setText(dateTimeFormatter.format(getItem(position)));
        return result;
    }
}
